<?php

namespace Kanin\Games\lib;

class Logs {

	/**
	 * @var \Kanin\Games\lib\Logs $_class
	 */
	private static $_class;

	private function __construct() { }

	public static function init(): Logs {
		if (!self::$_class instanceof Logs) {
			self::$_class = new Logs();
		}
		return self::$_class;
	}

	/**
	 * 写入
	 * @param mixed $content
	 * @param string $fileName
	 * @return false|int
	 */
	public function write($content, string $fileName = "debug.log") {
		if (is_array($content) || is_object($content)) {
			$content = var_export($content, true);
		}
		$path = PATH_DATA . 'logs/';
		if (!is_dir($path)) {
			mkdir($path, '755', true);
		}
		$fileName = $path . $fileName;
		$content = $content . PHP_EOL;
		return file_put_contents($fileName, $content, FILE_APPEND);
	}

	public function exLogWrite($code, $msg, $trace = '', $fileName = 'exLogs.log') {
		$content = <<<TEXT
错误码：{$code}
错误信息：{$msg}
调试trace：{$trace}
TEXT;
		return $this->write('[' . date('Y-m-d H:i:s') . '] ' . PHP_EOL . $content, $fileName);
	}
}